package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Meter;
import com.ruoyi.system.domain.MeterRecord;
import com.ruoyi.system.domain.vo.MeterRecordVO;
import com.ruoyi.system.domain.vo.MeterVO;
import com.ruoyi.system.mapper.MeterMapper;
import com.ruoyi.system.mapper.MeterRecordMapper;
import com.ruoyi.system.service.IMeterService;

/**
 * 电Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Service
public class MeterServiceImpl implements IMeterService 
{
    @Autowired
    private MeterMapper meterMapper;
    @Autowired
    private MeterRecordMapper meterRecordMapper;

    /**
     * 查询电
     * 
     * @param id 电主键
     * @return 电
     */
    @Override
    public Meter selectMeterById(Long id)
    {
        return meterMapper.selectMeterById(id);
    }

    /**
     * 查询电列表
     * 
     * @param meter 电
     * @return 电
     */
    @Override
    public List<MeterVO> selectMeterList(Meter meter)
    {
        List<MeterVO> selectMeterList = meterMapper.selectMeterList(meter);
        if (selectMeterList != null && selectMeterList.size() > 0 ) {
			for (MeterVO meterVO : selectMeterList) {
				Long id = meterVO.getId();
				MeterRecordVO meterRecord =new MeterRecordVO();
				meterRecord.setMeterId(id);
				MeterRecord selectMeterRecordSum = meterRecordMapper.selectMeterRecordSumNew(meterRecord );
				if (selectMeterRecordSum != null) {
					meterVO.setTotalValue(selectMeterRecordSum.getRechargeValue());
				}
			}
		}
		return selectMeterList;
    }

    /**
     * 新增电
     * 
     * @param meter 电
     * @return 结果
     */
    @Override
    public int insertMeter(Meter meter)
    {
        meter.setCreateTime(DateUtils.getNowDate());
        return meterMapper.insertMeter(meter);
    }

    /**
     * 修改电
     * 
     * @param meter 电
     * @return 结果
     */
    @Override
    public int updateMeter(Meter meter)
    {
        meter.setUpdateTime(DateUtils.getNowDate());
        return meterMapper.updateMeter(meter);
    }

    /**
     * 批量删除电
     * 
     * @param ids 需要删除的电主键
     * @return 结果
     */
    @Override
    public int deleteMeterByIds(Long[] ids)
    {
        return meterMapper.deleteMeterByIds(ids);
    }

    /**
     * 删除电信息
     * 
     * @param id 电主键
     * @return 结果
     */
    @Override
    public int deleteMeterById(Long id)
    {
        return meterMapper.deleteMeterById(id);
    }
}
